package com.example.anni.zhuzhoujia.xiaowan;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Anni on 2016/6/7.
 */
public class LoginActivity extends AppCompatActivity {
    private EditText etUserName; //账号
    private EditText etPassword; //密码
    private TextView tvZhuce; //注册
    private Button btLogin; //登录
    private CheckBox rememberPassword; //记住密码
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        etUserName = (EditText) findViewById(R.id.et_userName);
        etPassword = (EditText) findViewById(R.id.et_password);
        rememberPassword = (CheckBox) findViewById(R.id.chk_remember_password);
        sp = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
        rememberPassword.setChecked(false);
        initToolbar();
        rememberPassword();
        judgeCheck();
    }

    private void initToolbar() {
        TextView tvNameTitle;
        tvNameTitle = (TextView) findViewById(R.id.tv_name_title);
        tvNameTitle.setText("登录");
    }

    //注册
    public void initRegister(View v) {
        tvZhuce = (TextView) v.findViewById(R.id.tv_zhuce);
        tvZhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 0x002);
                overridePendingTransition(R.animator.override_change_animator_in, R.animator.override_change_animator_out);
            }
        });
    }

    public void login(View v) {
        //通过账号密码进行登录
        RequestParams requestParams =
                new RequestParams("http://tr.zzapi.gson.cn/api/user/login");
        requestParams.addBodyParameter("userName", etUserName.getText().toString());
        requestParams.addBodyParameter("password", etPassword.getText().toString());
        x.http().post(requestParams, new CallBackAdapter<String>() {
            @Override
            public void onSuccess(String s) {
                // Toast.makeText(LoginActivity.this, s, Toast.LENGTH_LONG).show();
                JSONObject json = JSON.parseObject(s);
                Boolean success = json.getBoolean("success");
                if (success) {

                    if (rememberPassword.isChecked()) {
                        editor = sp.edit();
                        editor.putString("userName", etUserName.getText().toString());
                        editor.putString("password", etPassword.getText().toString());
                        editor.commit();
                    }
                    editor.putString("area", json.getJSONObject("data").getString("area"));
                    editor.putString("avatar", json.getJSONObject("data").getString("avatar"));
                    editor.putString("dateline", json.getJSONObject("data").getString("dateline"));
                    editor.putString("email", json.getJSONObject("data").getString("email"));
                    editor.putString("gender", json.getJSONObject("data").getString("gender"));
                    editor.putString("id", json.getJSONObject("data").getString("id"));
                    editor.putString("lastUpdate", json.getJSONObject("data").getString("lastUpdate"));
                    editor.putString("latitude", json.getJSONObject("data").getString("latitude"));
                    editor.putString("longitude", json.getJSONObject("data").getString("longitude"));
                    editor.putString("nickName", json.getJSONObject("data").getString("nickName"));
                    editor.commit();
                    Intent intent = new Intent(LoginActivity.this, LogoActivity.class);
                    startActivity(intent);
                    //Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    /**
                     * 判断用户名和密码是否为空
                     */
                } else if (etUserName.getText().toString().equals("")) {
                    Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_LONG).show();
                } else if (etPassword.getText().toString().equals("")) {
                    Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
                } else if (etUserName.getText().toString().equals("") && etPassword.getText().toString().equals("")) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    //判断记住密码多选框的状态
    private void judgeCheck() {
        if (sp.getBoolean("isChecked", false)) {
            rememberPassword.setChecked(true);
            etUserName.setText(sp.getString("userName", ""));
            etPassword.setText(sp.getString("password", ""));
        }
    }

    //监听记住密码多选框按钮事件
    private void rememberPassword() {
        rememberPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (rememberPassword.isChecked()) {

                    //System.out.print("记住密码已选");
                    sp.edit().putBoolean("isChecked", true).commit();
                } else {

                    //System.out.print("记住密码没有选");
                    sp.edit().putBoolean("isChecked", false).commit();
                }
            }
        });
    }

}
